# environment setup
import numpy as np # Let's use numpy for this
from sympy.matrices import Matrix # Include this in case we want some pretty matrices
from sympy.solvers.solveset import linsolve
from sympy import * # import the entire namespace of sympy at root, NOT the best of practices
from math import e, pi
init_printing() # initialize pretty printing
# use linalg.solve to solve system
A = np.array([[1,1,1],[3,3,-1],[1,-1,1]])
B = np.array([-2,6,-1])
print('Solution: [u,v,w] =', np.linalg.solve(A,B))
# let's make this prettier with sympy
u,v,w = symbols('u v w')
# quick and clean
r=linsolve([u+v+w+2, 3*u+3*v-w-6, u-v+w+1], (u,v,w))
# more verbose
A = Matrix([[1,1,1],[3,3,-1],[1,-1,1]])
B = Matrix([-2,6,-1])
rr= linsolve((A,B), (u,v,w))
rr, r #show both results are the same, not really any prettier though
# (a) no solution (parallel)
h=3*3
k=0
print('h = ', h, ', k = ', k)
A = np.array([[1,3], [3,h]])
B = np.array([2,k])
try:
np.linalg.solve(A,B) # a Singular matrix is expected if no solution
except:
print('Singular matrix, no solution in this case, but not a suffienct test.')
# (a) no solution (parallel) continuned...
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np
x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (0 - 3*x1)/9
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '-g', label='$3x_1+9x_2=0$')
plt.title('System w/o Soluton')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()
# (b) a unique solution (beams are crossed)
h=3
k=0
print('h = ', h, ', k = ', k)
A = np.array([[1,3], [3,h]])
B = np.array([2,k])
try:
print('Solution', np.linalg.solve(A,B))
except:
print('Singular matrix, no solution or many solutions.')
# (b) a unique solution (beams are crossed) continuned...
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np
h=3
k=0
x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (k - 3*x1)/h
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '-g', label='$3x_1+3x_2=0$')
plt.title('System w/Unique Soluton')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()
print('BTW: How do you catch an unique animal?')
print(' Unique up on it, of course.')
# (c) many solutions (coeffs are of equal ratios, meaning lines are on top of each other)
h=3*3
k=2*3
print('h = ', h, ', k = ', k)
A = np.array([[1,3], [3,h]])
B = np.array([2,k])
try:
print('Solution', np.linalg.solve(A,B))
except:
print('Singular matrix, many solutions in this case.\nAgain not sufficient test but demonstrated graphically below.')
# (c) many solutions continuned...
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np
h=9
k=6
x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (k - 3*x1)/h
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '--g', label='$3x_1+9x_2=6$')
plt.title('System w/Many Solutons')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()
$4x_1 + x_2 + 3x_3 = 9$
$x_1 − 7x_2 − 2x_3 = 12$
$8x_1 + 6x_2 − 5x_3 = 15$
# first solve it
A = Matrix([[4,1,3],[1,-7,-2],[8,6,-5]])
B = Matrix([9,12,15])
x1,x2,x3= symbols('x_1 x_2 x_3')
xx= linsolve((A,B), (x1,x2,x3))
xx
# think about how best to show this using python
# of course we can just do this by hand, which we will
# use sympy to make some pretty stuff
v1=Matrix([[4],[1],[8]])
v2=Matrix([[1],[-7],[6]])
v3=Matrix([[3],[-2],[-5]])
B = Matrix([[9],[12],[15]])
x1,x2,x3= symbols('x_1 x_2 x_3')
A = x1*v1 + x2*v2 + x3*v3
(x1,x2,x3)=xx.args[0] # use result from above
BB = x1*v1 + x2*v2 + x3*v3
# show our varibles to demonstrate that A and B bits are equalivent to the system
[v1, v2, v3, A, B, BB, (x1,x2,x3) ]
# same idea as above but now with row vectors
# use sympy to make some pretty stuff
w1 = Matrix([[4,1,3]])
w2 = Matrix([[1,-7,-2]])
w3 = Matrix([[8,6,-5]])
b1 = 9
b2 = 12
b3 = 15
x1,x2,x3 = symbols('x_1 x_2 x_3')
x = Matrix([[x1], [x2], [x3]])
a1 = w1*x
a2 = w2*x
a3 = w3*x
# show our varibles to demonstrate that our system is equalivent
[w1,a1,b1], [w2,a2,b2], [w3,a3,b3], x, xx
$A= \begin{bmatrix} 1 & -2 & -6 \\ 0 & 3 & 6 \\ 1 &-2 & 5 \\ \end{bmatrix}, $ $B= \begin{bmatrix} 11 \\ -5 \\ 9 \\ \end{bmatrix}$
# by hand we would row reduce the augmented matrix and check if it has a solution
# here we simply show that a solution exits
A = np.array([[1,-2,-6],[0,3,6],[1,-2,5]])
B = np.array([11,-5,9])
np.linalg.solve(A,B) # it exists therefore it is a linear combination, isn't Linear Algebra fun with Python?!!
# 4. continued (use symp rref as example)
A = Matrix([[1,-2,-6],[0,3,6],[1,-2,5]])
B = Matrix([11,-5,9])
M=A.col_insert(4,B)
x1,x2,x3 = symbols('x_1 x_2 x_3')
Mrref = M.rref(pivots=False)
R = Mrref.col(-1)
print('Result as float:\n', np.array(Matrix.tolist(R)).astype(np.float64)) # show float numbers are same as above
Mrref.col(-1), linsolve((A,B), (x1,x2,x3)) # show rref and linsolve provide same result
# Don't we wish we did this HW problem before the quiz? Anyway, this is for the joy of it all.
# The operations again are translate, rotate and scale.
def f(z): return ( (z + (1+1j) * e**(-1j*pi/2)) * 2)
z=0
t= f(z)
a = -2j
b = 2-2j
tt= a * z + b
(t.real, t.imag), (tt.real, tt.imag) # show both hand calc and script are equal